﻿<?
/* $Id: Book.class.php 25 2011-02-18 16:22:57Z jim2212001@gmail.com $ */
if(!defined('WEBHOME'))
	die ('DO NOT EXECUTE THIS FILE');
require_once WEBHOME . '/libs/Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Books');
class Book
{
	function __construct(){
		$this->_books = new Zend_Gdata_Books();
		$this->results = 0;
		$this->_feed = NULL;
		$this->_entries = array();
	}
	
	public function query($q){
		$q = urlencode($q);
		$q = str_replace(' ','+',$q);
		$this->_query = $this->_books->newVolumeQuery();
		$this->_query->setQuery($q);
		$this->_feed = $this->_books->getVolumeFeed($this->_query);
		$this->_results = $this->_feed->getTotalResults();
		if($this->_results)
			$this->_processFeed();
		return $this->_entries;
	}
	
	public function getEntry($n){
		if($n>=$this->_results)$n=$this->_results-1;
		return $this->_entries[$n];
	}
	
	public function getEntries(){return $this->_entries;}
	
	public function getResults(){return $this->_results;}
	
	private function _processFeed(){
		foreach($this->_feed as $entry){
			array_push($this->_entries, new BookEntry($entry));
		}
		reset($this->_feed);
	}
	
	private $_books;
	private $_query;
	private $_feed;
	private $_results;
	private $_entries;
};
class BookEntry
{
	function __construct($entry){
		$thumb = $entry->getThumbnailLink();
		if($thumb)
			$this->_thumbLink = $entry->getThumbnailLink()->getHref();
		else
			$this->_thumbLink = 'site_media/images/pic-not-exist.png';
		$this->_infoLink = $entry->getInfoLink()->getHref();
		$this->_title = $this->_parseTitles($entry->getTitles());
		$this->_format = $this->_parseFormats($entry->getFormats());
		list($this->_ISBN10,$this->_ISBN13) = $this->_parseISBN($entry->getIdentifiers());
		$this->_publisher = implode(', ', $entry->getPublishers());
		$this->_author = implode(', ',$entry->getCreators());
		$this->_date = implode(', ',$entry->getDates());
	}
	
	public function getPublisher(){return $this->_publisher;}
	public function getAuthor(){return $this->_author;}
	public function getDate(){return $this->_date;}
	public function getThumbLink(){return $this->_thumbLink;}
	public function getInfoLink(){return $this->_infoLink;}
	public function getTitle(){return $this->_title;}
	public function getFormat(){return $this->_format;}
	public function getISBN10(){return $this->_ISBN10;}
	public function getISBN13(){return $this->_ISBN13;}
	
	private function _parseTitles($titles){
		$title = '<span class="book_title">'.$titles[0].'</span>';
		if(count($titles)==2)
			$title .= '<span class="book_subtitle">:'.$titles[1].'</span>';
		return $title;
	}
	
	private function _parseFormats($formats){
		if(count($formats)==2)
			$format = $formats[0].'('.$formats[1].')';
		else
		  	$format = '('.$formats[0].')';
		return $format;
	}
	
	private function _parseISBN($ISBNs){
		for($i=0;$i<2;$i++){
			$tmp=explode(':',$ISBNs[$i+1]);
			$ISBN[$i] = $tmp[1];
		}
		return $ISBN;
	}

	private $_publisher;
	private $_author;
	private $_date;
	private $_thumbLink;
	private $_infoLink;
	private $_title;
	private $_format;
	private $_ISBN10;
	private $_ISBN13;
};
?>
